bitkeeper revision 1.512 (3f8bcf2a0Vz2xXrdEA4lXWy2qRMaSg)
authoriap10@labyrinth.cl.cam.ac.uk <iap10@labyrinth.cl.cam.ac.uk>
Tue, 14 Oct 2003 10:25:46 +0000 (10:25 +0000)
committeriap10@labyrinth.cl.cam.ac.uk <iap10@labyrinth.cl.cam.ac.uk>
Tue, 14 Oct 2003 10:25:46 +0000 (10:25 +0000)
oops, forgot to click on xen_log to add it to repository.

.rootkeys
tools/misc/xen_log.c [new file with mode: 0644]

index 8555e6dadba0dfa1d931d43d534e28590cb60502..e4c9337047bf1667e7c77195695e161f4ae73a09 100644 (file)
--- a/.rootkeys
+++ b/.rootkeys
 3f5ef5a2dTZP0nnsFoeq2jRf3mWDDg tools/misc/xen-clone.README
 3f1668d4-FUY6Enc7MB3GcwUtfJ5HA tools/misc/xen-mkdevnodes
 3f870808zS6T6iFhqYPGelroZlVfGQ tools/misc/xen_cpuperf.c
+3f8bcf29ulZIC9rC4wM70H_q4s6VPg tools/misc/xen_log.c
 3f13d81eQ9Vz-h-6RDGFkNR9CRP95g tools/misc/xen_nat_enable
 3f13d81e6Z6806ihYYUw8GVKNkYnuw tools/misc/xen_nat_enable.README
 3f1668d4F29Jsw0aC0bJEIkOBiagiQ tools/misc/xen_read_console.c
diff --git a/tools/misc/xen_log.c b/tools/misc/xen_log.c
new file mode 100644 (file)
index 0000000..5adfa52
--- /dev/null
@@ -0,0 +1,171 @@
+#include <sys/types.h>
+#include <tcpd.h>
+#include <sys/socket.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <syslog.h>
+#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <sys/wait.h>
+#include <string.h>
+
+#include "hypervisor-ifs/dom0_ops.h"
+#include "dom0_defs.h"
+#include "mem_defs.h"
+
+#define SILENT_ERRORS_FROM_XEN
+#define SYSLOG 1
+#define SYSLOGTO LOG_LOCAL5
+
+int logoutput;
+
+void stripit(char *str)
+{
+  register int i;
+
+  for (i = 0; str[i]; i++) {
+      if (str[i] == '\n') str[i] = '\0';
+      if (str[i] == '\r') str[i] = '\0';
+  }
+}
+
+void errexit(char *str)
+{
+    if(logoutput == SYSLOG) {
+        stripit(str);
+        syslog(LOG_ERR, "%s failed: %d (%m)", str, errno);
+    } else {
+        printf("%s", str);
+    }
+    exit(1);
+}
+
+void log(char *str)
+{
+    if(logoutput == SYSLOG) {
+        stripit(str);
+        syslog(LOG_INFO, "%s", str);
+    } else {
+        printf("%s", str);
+    }
+}
+
+void process()
+{
+    dom0_op_t op;
+    unsigned char buf[208], obuf[224];
+    struct sockaddr_in addr, from;
+    int fromlen = sizeof(from);
+    int len, fd = socket(PF_INET, SOCK_DGRAM, 0);
+    unsigned short int lastport = 0, curport = 0;
+    
+    if ( fd < 0 )
+        errexit("could not open datagram socket");
+
+    memset(&addr, 0, sizeof(addr));
+    addr.sin_addr.s_addr = htonl(0xa9fe0100); /* 169.254.1.0 */
+    addr.sin_port = htons(666);
+    addr.sin_family = AF_INET;
+
+    if ( bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0 )
+        errexit("could not bind to local address and port");
+
+    op.cmd = DOM0_GETDOMAININFO;
+
+    while ( (len = recvfrom(fd, buf, sizeof(buf), 0,
+            (struct sockaddr *)&from, &fromlen)) >= 0 )
+    {
+        curport = ntohs(from.sin_port);
+        if(lastport != curport) {
+           op.u.getdominfo.domain = (int)curport;
+           if ( do_dom0_op(&op) < 0 )
+              log("Error resolving domain name\n");
+           } else {
+              lastport = curport;
+           }
+        }
+
+        sprintf(obuf, "[%s] %s", op.u.getdominfo.name, curport, buf);
+       log(obuf);
+
+        fromlen = sizeof(from);
+    }
+}
+
+void closeall(int fd)
+{
+    int fdlimit = sysconf(_SC_OPEN_MAX);
+
+    while (fd < fdlimit)
+      close(fd++);
+}
+
+int daemon(int nochdir, int noclose)
+{
+    switch (fork())
+    {
+        case 0:  break;
+        case -1: return -1;
+        default: _exit(0);
+    }
+
+    if (setsid() < 0)
+      return -1;
+
+    switch (fork())
+    {
+        case 0:  break;
+        case -1: return -1;
+        default: _exit(0);
+    }
+
+    if (!nochdir)
+      chdir("/");
+
+    if (!noclose)
+    {
+        closeall(0);
+        open("/dev/null",O_RDWR);
+        dup(0); dup(0);
+    }
+
+    return 0;
+}
+
+int main(int argc, char **argv)
+{
+    logoutput = 0;
+    int c;
+
+    opterr = 0;
+
+    while ((c = getopt (argc, argv, "dh")) != -1)
+    {
+        switch(c)
+        {
+            case 'd':
+                logoutput = SYSLOG;
+                if (daemon(0,0) < 0)
+                {
+                    errno = 2;
+                    errexit("daemon");
+                } else {
+                    openlog("xenolog", LOG_PID, SYSLOGTO);
+                }
+                break;
+            case 'h':
+                printf("Usage: xenolog [options]\n");
+                printf("Capture and display output of xen domains.\n\n");
+                printf("  -d       Daemonize and send output to syslog.\n");
+                exit(0);
+                break;
+        }
+    }
+
+    process();
+
+    return 0;
+}
+